package com.huitone.gddw.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReplaceUtil {
	
	private static Pattern pattern;

    static {
        pattern = Pattern.compile("((?<=\\{)([a-zA-Z_]{1,})(?=\\}))");
    }
	
	public static String replaceSMS(String text, Map<String, Object> map) {
        Matcher matcher = pattern.matcher(text);
        while (matcher.find()) {
            String key = matcher.group();
            text = text.replaceAll("\\{" + key + "\\}", map.get(key) + "");
        }

        return text;
    }
	
	
	public static void main(String[] args) {
		// 生成6为随机数
		String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);
		String time = "2";
		String text = "{verifyCode}为您的登录验证码，请于{time}分钟内填写。如非本人操作，请忽略本短信。";
		Map<String, Object> map = new HashMap<>(2);
        map.put("verifyCode", verifyCode);
        map.put("time", time);

        String res = replaceSMS(text, map);
        System.out.println(res); 
	}
}
